package com.zha.boss.mvc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zha.boss.dao.entity.UserAccount;
import com.zha.boss.dao.entity.UserInfo;
import com.zha.boss.dao.mapper.UserAccountMapper;
import com.zha.boss.dao.mapper.UserInfoMapper;
import com.zha.boss.enums.resp.RespCode;
import com.zha.boss.enums.status.PublicStatus;
import com.zha.boss.exceptions.BossException;
import com.zha.boss.mvc.models.forms.UserEditForm;
import com.zha.boss.mvc.service.UserService;
import com.zha.boss.util.NoBuilderUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author: zha
 * @CreateTime: 2024年09月11日 17:39
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private UserAccountMapper userAccountMapper;

    @Override
    public IPage<UserInfo> pageList(Integer pageNo, Integer pageSize) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.orderByDesc("create_time");
        return userInfoMapper.selectPage(Page.of(pageNo, pageSize),wrapper);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public synchronized void userRegisterEdit(UserEditForm userEditForm) {
        // 用service处理注册或修改,loginName的值不能修改
        if (StrUtil.isBlank(userEditForm.getId())){ // 没有主键,是注册新用户
            // 登录名称是唯一的
            QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
            wrapper.eq("login_name", userEditForm.getLoginName());
            UserInfo userInfo = userInfoMapper.selectOne(wrapper);
            if (userInfo!= null){
                throw new BossException(RespCode.USER_LOGIN_NAME_EXITS);
            }
            // 注册新用户和开通资金账号
            addUserAndAccount(userEditForm);
        }else {  // 修改已有的用户信息
            UserInfo userInfo = userInfoMapper.selectById(userEditForm.getId());
            if (userInfo == null){
                throw new BossException(RespCode.USER_NOT_EXISTS);
            }
            UserInfo updateInfo = new UserInfo();
            updateInfo.setId(userInfo.getId());
            updateInfo.setUserEmail(userEditForm.getUserEmail());
            updateInfo.setShopName(userEditForm.getShopName());
            updateInfo.setShopScope(userEditForm.getShopScope());
            updateInfo.setPhone(userEditForm.getPhone());

            userInfoMapper.updateById(updateInfo);

        }
    }

    @Override
    public UserInfo queryUserById(String id) {
        return userInfoMapper.selectById(id);
    }

    @Override
    public void editUserStatus(String userNo) {
        // 查询当前用户的状态,修改为相反值
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("user_no", userNo);
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);
        if (userInfo == null){
            throw new BossException(RespCode.USER_NOT_EXISTS);
        }
        String newStatus = PublicStatus.ENABLE.name();
        if (PublicStatus.ENABLE.name().equals(userInfo.getStatus())){
            newStatus = PublicStatus.DISABLE.name();
        }
        UpdateWrapper<UserInfo> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("user_no", userNo)
                .set("status", newStatus);
        userInfoMapper.update(updateWrapper);
    }

    @Override
    public List<UserInfo> matchShopName(String shopName) {
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.select("user_no","shop_name").like("shop_name", shopName)
                .orderByAsc("shop_name");
        return userInfoMapper.selectList(wrapper);
    }


    private void addUserAndAccount(UserEditForm userEditForm) {
        UserInfo userInfo;
        // 可以添加新用户
        userInfo = new UserInfo();
        userInfo.setId(NoBuilderUtil.getUUID());
        userInfo.setUserNo(NoBuilderUtil.getUserNo());
        userInfo.setLoginName(userEditForm.getLoginName());
        userInfo.setAccountNo(NoBuilderUtil.getAccountNo());
        userInfo.setPhone(userEditForm.getPhone());
        userInfo.setPassword(NoBuilderUtil.getUUID());
        userInfo.setPayPwd(NoBuilderUtil.getUUID());
        userInfo.setShopName(userEditForm.getShopName());
        userInfo.setShopScope(userEditForm.getShopScope());
        userInfo.setUserEmail(userEditForm.getUserEmail());
        userInfo.setStatus(PublicStatus.DISABLE.name());
        userInfo.setCreateTime(new Date());
        userInfo.setRemark("创建商户");
        userInfoMapper.insert(userInfo);
        // 创建资金账号
        UserAccount userAccount = new UserAccount();
        userAccount.setId(NoBuilderUtil.getUUID());
        userAccount.setUserNo(NoBuilderUtil.getUserNo());
        userAccount.setAccountNo(NoBuilderUtil.getAccountNo());
        userAccount.setBalance(new BigDecimal(0));
        userAccount.setTotalIncome(new BigDecimal(0));
        userAccount.setTotalExpend(new BigDecimal(0));
        userAccount.setStatus(PublicStatus.DISABLE.name());
        userAccount.setRemark("创建资金账号");
        userAccount.setEditTime(new Date());
        userAccountMapper.insert(userAccount);
    }
}
